using Microsoft.VisualStudio.TestTools.UnitTesting;
using Evaluant.Uss;
using Evaluant.Uss.Memory;


namespace TestUSS
{
	[TestClass]
	public class TestMemoryPersistenceEngine : Common
	{
		protected override void CreatePersistenceEngine()
		{
			pef = XmlConfigLoader.LoadXmlConfig("engines.config", "Memory");
		}

		[TestMethod]
		public void TestWithDelegator()
		{
			MemoryProvider mp1 = new MemoryProvider();
			mp1.RegisterMetaData(MetaDataFactory.FromMetaDataFile(@"..\..\..\NUnit\PersistenceEngine\Models\Common.xml"));
			mp1.InitializeConfiguration();

			IPersistenceEngine pe1 = mp1.CreatePersistenceEngine();
			pe1.InitializeRepository();

			EntitySet es = new EntitySet();
			for(int i=0; i<10; i++)
			{
				Entity p = new Entity("Person");
				p.AddValue("Firstname", "p" + i.ToString());
				p.AddValue("Age", i);
				es.Add(p);
			}

			Transaction t = new Transaction();
			t.Serialize(es);
			t.Commit(pe1);

			MemoryProvider mp2 = new MemoryProvider(mp1);
			mp2.RegisterMetaData(MetaDataFactory.FromMetaDataFile("..\\..\\..\\NUnit\\PersistenceEngine\\Models\\Common.xml"));
			mp2.InitializeConfiguration();

            MemoryEngine pe2 = mp2.CreatePersistenceEngine() as MemoryEngine;
			pe2.Delegator = pe1;

			es = pe2.Load("Person[Firstname ^ 'p']");
			Assert.AreEqual(10, es.Count);

			Entity p11 = new Entity("Person");
			p11.AddValue("Firstname", "p11");
			p11.AddValue("Age", 11);
			
			t = new Transaction();
			t.Serialize(p11);
			t.Commit(pe2);

			es = pe2.Load("Person[Firstname ^ 'p']");
			Assert.AreEqual(11, es.Count);

			es = pe1.Load("Person[Firstname ^ 'p']");
			Assert.AreEqual(11, es.Count);

			Entity p12 = new Entity("Person");
			p12.AddValue("Firstname", "p12");
			p12.AddValue("Age", 12);
			
			t = new Transaction();
			t.Serialize(p12);
			t.Commit(pe1);

			es = pe1.Load("Person[Firstname ^ 'p']");
			Assert.AreEqual(12, es.Count);

			es = pe2.Load("Person[Firstname ^ 'p']");
			Assert.AreEqual(11, es.Count);
		}

        public void Test()
        {
            base.OPath();
        }
	}
}
